package com.chujian.Activities;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.view.View;
import android.view.Window;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.chujian.Threads.HttpClientPostThread;
import com.chujian.Utils.Constan;
import com.chujian.Utils.JsonUtil;
import com.chujian.model.Table;
import com.chujian.orderingsystem.R;
import com.google.gson.Gson;

public class TableAddActivity extends Activity{
//	餐桌修改标志
	private boolean modifyFlag=false;
	private Table mTable;
	private EditText mEtTableName;
	private EditText mEtTableCapacity;
	private TextView mTvTitle;
	private String mName;
	private int mCapacity;
	private Handler mHandler=new Handler(){
		@Override
		public void handleMessage(android.os.Message msg) {
			super.handleMessage(msg);
			String str=(String) msg.obj;
			 if(str.equals("addFailed")){
				Toast.makeText(TableAddActivity.this,"添加失败", Toast.LENGTH_LONG).show();
			}else if(str.equals("updateSuccess")){
				Toast.makeText(TableAddActivity.this,"修改成功", Toast.LENGTH_LONG).show();
				Intent intent=new Intent();
//				intent.setClass(TableAddActivity.this, TableListActivity.class);
				Bundle bundle=new Bundle();
				bundle.putSerializable("table1", mTable);//序列化  
				intent.putExtras(bundle);
				setResult(1001, intent);
				TableAddActivity.this.finish();
			}else if(str.equals("updateFailed")){
				Toast.makeText(TableAddActivity.this,"修改失败", Toast.LENGTH_LONG).show();
			}else {
				Toast.makeText(TableAddActivity.this,"添加成功", Toast.LENGTH_LONG).show();
//		                       将数据进行返回
				Intent intent=new Intent();
//				intent.setClass(TableAddActivity.this, TableListActivity.class);
				Bundle bundle=new Bundle();
				Table tableAdd=JsonUtil.TableFromJson(str);
				bundle.putSerializable("table1", tableAdd);//序列化  
				intent.putExtras(bundle);
				setResult(1000, intent);
				TableAddActivity.this.finish();
			}
		};
	};
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);//去掉标题栏
		setContentView(R.layout.table_add);
		initView();
		
	}
	private void initView() {
		// TODO Auto-generated method stub
		mEtTableName=(EditText) findViewById(R.id.et_table_add_name);
		mEtTableCapacity=(EditText) findViewById(R.id.et_table_add_capacity);
		mTvTitle=(TextView) findViewById(R.id.tv_table_add_title);
		
		Intent intent =getIntent();
		mTable=(Table) intent.getSerializableExtra("table");
		if(mTable!=null){
			mEtTableName.setText(mTable.getTableName());
			mEtTableCapacity.setText(mTable.getTableCapacity()+"");
			mTvTitle.setText("修改餐桌信息");
			modifyFlag=true;
		}
	}
	public void doClick(View v){
		switch (v.getId()) {
		case R.id.btn_table_add_cancel:
			TableAddActivity.this.finish();
			break;
			
		case R.id.btn_table_add_confirm:
			String tName=mEtTableName.getText().toString();
			String tCapacity=mEtTableCapacity.getText().toString().trim();
			if(tName.equals("")||tCapacity.equals("")){
				Toast.makeText(TableAddActivity.this,"请将数据填充完整！", Toast.LENGTH_SHORT).show();
				break;
			}
			Table table=new Table();
			try {
				Integer capacity=Integer.parseInt(tCapacity);
				mName=tName;
				mCapacity=capacity;
				table.setTableCapacity(capacity);
				table.setTableName(tName);
				if(modifyFlag){
					mTable.setTableCapacity(capacity);
					mTable.setTableName(tName);
				}
			} catch (NumberFormatException e) {
				Toast.makeText(TableAddActivity.this,"数据格式错误，请重新输入！", Toast.LENGTH_SHORT).show();
				e.printStackTrace();
				break;
			}
			
			Gson gson=new Gson();
			String url=null;
			String jsonString=null;
			if(modifyFlag){
				url=Constan.BASE_URL+"TableModifyServlet";
				jsonString=gson.toJson(mTable);
			}else{
				 jsonString=gson.toJson(table);
				 url=Constan.BASE_URL+"TableAddServlet";
			}
			new HttpClientPostThread(url, mHandler, jsonString).start();
			break;
		default:
			break;
		}
	}
}
